/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package exterminadordeburbujas;

import javax.swing.JButton;
import javax.swing.JFrame;

/**
 *
 * @author Jose Gabriel
 */
public class Gui extends JFrame {
    
    private Burbuja burbujas[][];
    private final int COLUMNAS = 5;
    private final int FILAS = 5;
    private JButton reiniciar;
    
    public Gui(){
        this.getContentPane().setLayout(null); 
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setVisible(true);
        this.setBounds(0, 0, 300, 300); 
        
        burbujas = new Burbuja[COLUMNAS][FILAS];
        
        for(int i=0, posx=0, posy=0; i<COLUMNAS  ;i++){
            for(int j=0 ; j<FILAS; j++, posx++){
                burbujas[i][j] = new Burbuja(5,((int)(Math.random()*5)));
                burbujas[i][j].setBounds(51*j+10, 31*i+10, 50, 30);
                burbujas[i][j].setActionCommand(i+""+j+"");
                burbujas[i][j].iniciar();
                this.add(burbujas[i][j]);
            }
        }
        
        /*reiniciar = new JButton();
        reiniciar.setBounds(400, 200, 100, 30);
        reiniciar.setText("Reiniciar?");
        reiniciar.setVisible(false);
        this.add(reiniciar);*/

    }
    
    public void reiniciar(){
        //reiniciar.setVisible(true);
    }
    
    public void transferirDer(int gas,int i, int j){
        for( ;i<COLUMNAS && i>=0;i++){
            if ( !burbujas[i][j].isEmpty() && !burbujas[i][j].isFull()){
                
                burbujas[i][j].recibir(gas);
                if(burbujas[i][j].isFull()){
                    burbujas[i][j].transferir(i, j);
                }
                return;
            }
        }
    }
    
    public void transferirIzq(int gas,int i, int j){
        for( ;i<COLUMNAS && i>=0;i--){
            if ( !burbujas[i][j].isEmpty() && !burbujas[i][j].isFull()){
                burbujas[i][j].recibir(gas);
                if(burbujas[i][j].isFull()){
                    burbujas[i][j].transferir(i, j);
                }                
                return;
            }
        }
    }
 
     public void transferirAba(int gas,int i, int j){
        for( ; j<FILAS && j>=0 ;j++){
            if ( !burbujas[i][j].isEmpty() && !burbujas[i][j].isFull()){
                burbujas[i][j].recibir(gas);
                if(burbujas[i][j].isFull()){
                    burbujas[i][j].transferir(i, j);
                }                
                return;
            }
        }
    }
    
    public void transferirArr(int gas,int i, int j){
        for( ; j<FILAS && j>=0 ;j--){
            if ( !burbujas[i][j].isEmpty() && !burbujas[i][j].isFull()){
                burbujas[i][j].recibir(gas);
                if(burbujas[i][j].isFull()){
                    burbujas[i][j].transferir(i, j);
                }                      
                return;
            }
        }
    }   
    
    
}
